package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.qu_tao.R;
import com.example.qu_tao.adapter.GoodsListAdapter;
import com.example.qu_tao.contract.ShouyeContract;
import com.example.qu_tao.database.GoodsDao;
import com.example.qu_tao.entity.Goods;
import com.example.qu_tao.presenter.ShouyePersenter;

import java.util.ArrayList;
import java.util.List;

public class ShouyeFragment extends BaseFragment implements ShouyeContract.IShouyeView {
    private EditText mshouyesousuo;
    private RecyclerView mshouyeList;
    private GoodsListAdapter mRecyclerAdapter;
    private ShouyeContract.IShouyeContract mShouyeContract;
    private List<Goods> mGoodsList;
    private TextView mShouyetiaozhuan;
    private static final String TAG = "ShouyeFragment";
    private GoodsDao goodsDao;

    @Override
    protected int getLayoutId() {
        return R.layout.fargment_shouye;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        mShouyeContract = new ShouyePersenter(this, getContext());
        // 绑定首页部件
        mshouyesousuo = mView.findViewById(R.id.shouye_sousuokuang);
        mGoodsList = new ArrayList<>();
        mshouyeList = mView.findViewById(R.id.shouye_List);
        mShouyetiaozhuan = mView.findViewById(R.id.shouye_tiaozhuan);

        // 瀑布流显示 两列
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mshouyeList.setLayoutManager(staggeredGridLayoutManager);
        // 修改点：传递上下文到适配器
        mRecyclerAdapter = new GoodsListAdapter(requireContext(), mGoodsList);
        mshouyeList.setAdapter(mRecyclerAdapter);

        // 添加搜索监听器
        mshouyesousuo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String keyword = s.toString().trim();
                mShouyeContract.searchGoods(keyword);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        // 跳转
        mShouyetiaozhuan.setOnClickListener(v -> {
            FragmentManager fragmentManager = getParentFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_content, new MianjiaShouyeFragment(), "MianjiaShouyeFragment");
            transaction.addToBackStack(null);
            transaction.commit();

            // 更新底部导航栏选中状态
            if (getActivity() instanceof MainActivity) {
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.updateBottomNavSelection(R.id.main_shouye);
            }
            ((MainActivity) getActivity()).switchToSellerMode(true);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mShouyeContract.getGoodsList();
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void showGoodsLisst(List<Goods> goodsList) {
        mGoodsList = goodsList;
        Log.d(TAG, "showGoodsList: "+mGoodsList.size());
        mRecyclerAdapter.updateData(mGoodsList);
    }

}